#ifndef __MANAGER_H__
#define __MANAGER_H__

#include "ClientSdk.h"
#include "CmdChannel.h"
#include "MediaChannel.h"
#include "WhiteBoardManager.h"
#include "define.h"
#include "ListPtr.h"
#include "Lock.h"
#include "MyEvent.h"
#include "VideoEncoder.h"
#include "H264Decoder.h"
#include "SDL.h"
#include "VideoCapture.h"
#include "AudioManager.h"

class CManager : public CMyThread
	, EncodeDataNotify
	, DecodeDataNotify
	, VideoCaptrueDataNotify
{
public:
	bool StartApp(const char *pSvrIp, unsigned short usPort);
	void Stop(void);

	bool SetPlayWindows(unsigned long ulLocalVideo, unsigned long ulRemoteVideo);

	uint32_t GetWindowHandle(void);
	void ClearLocalWbData(void);

	void SetWbMode(bool bMode);

	void EnableDataOperationRight(bool bEnable);
	void EnadleSync(bool bEnable);

	bool ReplyScreenBroadcastReq(int nUserId, int nResult);

	int InviteOpenScreenBroadcast(int nUserId, bool bForce);
	int ScreenBroadcastControl(int nType);

	void PutRecvData(RecvDataInfo *pRecv);
	void PutScreenBroadcastData(RecvDataInfo *pRecv);

	int UpdataFileInfo(const char *pFilePath);
	bool SendChatMessage(const char *pMessage);

	int	InitVideoCapture(void);
	int UpdateVideoCapture(void);

	int GetVideoCaptrueDevice(char *pData, int *pDataLen);

	int StartCaptrue(int nIndex, int nWidth, int nHeight, int nFrameRate);
	int StartCaptrueEx(const char *pUIDName, int nWidth, int nHeigth, int nFrameRate);

	int StopCapture(int nIndex);
	void ShowSetDialogBox(const char *pUIDName, const char *pTitle, void *pParentWindow, int nX, int nY);
	void ShowSetDialogBoxEx(int nIndex, const char *pTitle, void *pParentWindow, int nX, int nY);
public:
	CManager(int nTerminalId, const char *pSaveFilePath, OnMessageCallback callback, void *pUser);
	~CManager(void);	

	char *GetSvrIp(void);
	unsigned short GetSvrPort(void);

	int	GetTerminalId(void);
	string GetFileSavePath(void);	

	int SendDataByMediaChannel(int nCmd, const char *pData, int nDataLen);
	void HandleRecvWBData(const char *pData, int nDataLen);

	void HandleRecvFileEnd(void);
	void NoticeMessage(int nType, const char *pMsg);
protected:
	virtual void ThreadProcMain(void);
	//virtual void OnCaptureNotify(const char *pData, const long nLen, const ScreenParams *pParam);

	virtual void OnEncodeData(H264VideoParam *videoData);
	virtual void OnDecodeData(int nId, int nFrameRate, AVFrame *pAvFrame);

	virtual void OnCaptureData(int nId, const uint8_t * const pFrame[], int nWidth, int nHeight, uint64_t ulTimestamp);
private:
	void HandleRecvData(RecvDataInfo *pRecv);
	bool InitVideoRender(int nWidth, int nHeight);

	void HandleExtSignalControl(RecvDataInfo *pRecv);

	void HandleStopScreen(RecvDataInfo *pRecv);
	void HandleInviteScreenCast(RecvDataInfo *pRecv);

	void HandleMeetingInfo(RecvDataInfo *pRecv);

	void TestAudio(void);

private:
	bool				m_bExit;
	unsigned long		m_ulRemoteVideo;

	CMediaChannel		*m_pMedia;
	CCmdChannel			*m_pCmd;

	CWhiteBoardManager	*m_pWb;
	char				*m_pVideoBuf;

	int					m_nWidth;
	int					m_nHeight;

	ListPtr				m_lstRecvData;
	CLock				m_lockRecvData;

	char				m_szSvrIp[MAX_IPADRESS_LEN];
	unsigned short		m_usSvrPort;

	unsigned long		m_ulLastSendTime;
	bool				m_bConnect;

	int					m_nTerminalId;
	string				m_strSavePath;

	OnMessageCallback	m_pCallback;
	void				*m_pUser;

	CVideoEncoder		*m_pVideoEncoder;
	CH264Decoder		*m_pVideoDecoder;

	int					m_nCount;
	SDL_Surface			*m_pScreen;

	SDL_Overlay			*m_pBmp;
	bool				m_bInitRender;

	char				m_szTermianlId[12];
	CVideoCapture		*m_pVideoCapture;

	int					m_nOutWidth;
	int					m_nOutHeigth;

	CLock				m_lockSendVideoSignal;
	bool				m_bSendVideoSignal;

	bool				m_bScreen;
	bool				m_bFirst;

	CAudioManager		*m_pAudioMan;
};
#endif
